package model;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LopHoc {
	private List<HocSinh> listHocSinh;
	
	//constructor
	public LopHoc() {
		setListHocSinh(new ArrayList<HocSinh>());
	}
	
	//nhap danh sach hoc sinh va thong tin
	public void nhapDanhSach() {
		Scanner s = new Scanner(System.in);
		for(int i = 0; i < 2; i++) {
			int ms;
			String ht;
			float dtb;
			
			//nhap ma so
			System.out.print("Mã số: ");
			do {
				ms = s.nextInt();
			} while(kiemTraMaSo(ms) || ms <= 0);
			
			//nhap ho ten
			System.out.print("Họ tên: ");
			do {
				ht = s.nextLine();
			} while(ht.trim().isEmpty() || ht == "");
			
			//nhap diem trung binh
			System.out.print("Điểm trung bình: ");
			do {
				dtb = s.nextFloat();
			} while(dtb < 0 && dtb > 10);
		}
		
	}
	
	//ham kiem tra ma so hoc sinh co ton tai hay chua
	public boolean kiemTraMaSo(int ms) {
		for(HocSinh hs: listHocSinh) {
				if(hs.getMaSo() == ms) {
					return true;
				}
		}
		return false;
	}
	
	//ham sap xep hoc sinh theo thu tu giam dan cua diem trung binh
	public void sapXep() {
		for(int i = 0; i < listHocSinh.size() - 1; i++) {
			for(int j = i + 1; j < listHocSinh.size(); j++) {
				HocSinh temp = new HocSinh();
				if(listHocSinh.get(i).getDiemTrungBinh() < listHocSinh.get(j).getDiemTrungBinh()) {
					temp.setMaSo(listHocSinh.get(i).getMaSo());
					temp.setHoTen(listHocSinh.get(i).getHoTen());
					temp.setDiemTrungBinh(listHocSinh.get(i).getDiemTrungBinh());
					
					listHocSinh.get(i).setMaSo(listHocSinh.get(j).getMaSo());
					listHocSinh.get(i).setHoTen(listHocSinh.get(j).getHoTen());
					listHocSinh.get(i).setDiemTrungBinh(listHocSinh.get(j).getDiemTrungBinh());
					
					listHocSinh.get(j).setMaSo(temp.getMaSo());
					listHocSinh.get(j).setHoTen(temp.getHoTen());
					listHocSinh.get(j).setDiemTrungBinh(temp.getDiemTrungBinh());
				}
			}
		}
	}
	
	public void xepHang() {
		for(int i = 0; i < listHocSinh.size(); i++) {
			System.out.println("Hang " + (i + 1) + ": ");
			System.out.print("\tMa so: " + listHocSinh.get(i).getMaSo());
			System.out.println("Ho ten: " + listHocSinh.get(i).getHoTen());
			System.out.print("\tDiem trung binh: " + listHocSinh.get(i).getDiemTrungBinh());			
		}
	}

	public List<HocSinh> getListHocSinh() {
		return listHocSinh;
	}

	public void setListHocSinh(List<HocSinh> listHocSinh) {
		this.listHocSinh = listHocSinh;
	}
	
	
	
	

}
